package com.ruoyi.check.service.impl;

import java.util.List;

import com.ruoyi.check.domain.BmCheckItem;
import com.ruoyi.check.service.IBmCheckItemService;
import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.check.mapper.BmCheckMapper;
import com.ruoyi.check.domain.BmCheck;
import com.ruoyi.check.service.IBmCheckService;

/**
 * 库存盘点Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-08-07
 */
@Service
public class BmCheckServiceImpl implements IBmCheckService 
{
    @Autowired
    private BmCheckMapper bmCheckMapper;

    @Autowired
    private IBmCheckItemService bmCheckItemService;

    /**
     * 查询库存盘点
     * 
     * @param id 库存盘点主键
     * @return 库存盘点
     */
    @Override
    public BmCheck selectBmCheckById(Long id)
    {
        List<BmCheckItem> bmCheckItems = bmCheckItemService.selectByCheckIdBmCheckItems(id);
        BmCheck bmCheck = bmCheckMapper.selectBmCheckById(id);
        bmCheck.setGoodsList(bmCheckItems);
        return bmCheck;
    }

    /**
     * 查询库存盘点列表
     * 
     * @param bmCheck 库存盘点
     * @return 库存盘点
     */
    @Override
    public List<BmCheck> selectBmCheckList(BmCheck bmCheck)
    {
        return bmCheckMapper.selectBmCheckList(bmCheck);
    }

    /**
     * 新增库存盘点
     * 
     * @param bmCheck 库存盘点
     * @return 结果
     */
    @Override
    public void insertBmCheck(BmCheck bmCheck)
    {
        bmCheckMapper.insertBmCheck(bmCheck);
        bmCheck.setCreateTime(DateUtils.getNowDate());
        List<BmCheckItem> goodsList = bmCheck.getGoodsList();
        for (int i = 0; i < goodsList.size(); i++) {
            goodsList.get(i).setCheckId(bmCheck.getId());
            bmCheckItemService.insert(goodsList.get(i));
        }
    }

    /**
     * 修改库存盘点
     * 
     * @param bmCheck 库存盘点
     * @return 结果
     */
    @Override
    public int updateBmCheck(BmCheck bmCheck)
    {
        return bmCheckMapper.updateBmCheck(bmCheck);
    }

    /**
     * 批量删除库存盘点
     * 
     * @param ids 需要删除的库存盘点主键
     * @return 结果
     */
    @Override
    public int deleteBmCheckByIds(Long[] ids)
    {
        for (int i = 0; i < ids.length; i++) {
            bmCheckItemService.deleteByPrimaryKey(ids[i]);
        }
        return bmCheckMapper.deleteBmCheckByIds(ids);
    }

    /**
     * 删除库存盘点信息
     * 
     * @param id 库存盘点主键
     * @return 结果
     */
    @Override
    public int deleteBmCheckById(Long id)
    {
        bmCheckItemService.deleteByPrimaryKey(id);
        return bmCheckMapper.deleteBmCheckById(id);
    }

    @Override
    public int updateStatus(BmCheck bmCheck) {
        if (bmCheck.getStatus() == 1) {
            return bmCheckMapper.updateStatusTwo(bmCheck);
        } else {
            return bmCheckMapper.updateStatus(bmCheck);
        }
    }
}
